package com.vivo.tasks.vivofx;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.vivo.common.DatabaseContextHolder;
import com.vivo.fx.retail.service.FxDeaRetailorderService;
import com.vivo.retail.entity.VivoRetailOrder;
import com.vivo.retail.service.VivoRetailOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class TaskRetail {

    @Autowired
    private FxDeaRetailorderService fxDeaRetailorderService;

    @Autowired
    private VivoRetailOrderService vivoRetailOrderService;


    public void SyncData() {
        GetRetailOrder("ah");
        GetRetailOrder("js");
    }

    private void GetRetailOrder(String area) {
        System.out.println("get retail data from " + area + " db start...");
        Calendar cal = Calendar.getInstance();
        Date startDate = null;
        Date endDate = new Date();

        VivoRetailOrder lastItem = new VivoRetailOrder();
        lastItem.setTableZone(area);
        lastItem = vivoRetailOrderService.getLast(lastItem);

        if (lastItem == null) {
            // 起始时间为2016年6月1日
            cal.set(2016, Calendar.JULY, 1);
            startDate = cal.getTime();
        } else {
            startDate = lastItem.getMakeDate();
        }

        System.out.println("last change date is :" + startDate);

        int pageStart = 1;
        int pageSize = 5000;


        VivoRetailOrder queryOrder = new VivoRetailOrder();
        queryOrder.setBeginMakeDate(startDate);
        queryOrder.setEndMakeDate(endDate);

        Page<VivoRetailOrder> pageParam = new Page<VivoRetailOrder>(pageStart, pageSize);
        pageParam.setOrderBy("b.makedate");

        try {
            // 切换数据库
            Helper.changeDB(area);

            Page<VivoRetailOrder> pageRes = fxDeaRetailorderService.findPage(pageParam, queryOrder);
            DatabaseContextHolder.setMainDb();

            SaveRetailOrder(pageRes.getList(), area);

            for (int i = pageStart + 1; i <= pageRes.getTotalPage(); i++) {
                System.out.println("total page is : " + pageRes.getTotalPage() + " cur page is :" + pageRes.getPageNo() + " and pageSize is :" + pageRes.getPageSize());

                pageParam.setPageNo(i);

                // 切换数据库
                Helper.changeDB(area);

                pageRes = fxDeaRetailorderService.findPage(pageParam, queryOrder);
                DatabaseContextHolder.setMainDb();

                SaveRetailOrder(pageRes.getList(), area);
            }
        } finally {
            DatabaseContextHolder.setMainDb();
        }

        System.out.println("get data from " + area + " db end...");
    }


    private void SaveRetailOrder(List<VivoRetailOrder> retailorders, String area) {
        List<VivoRetailOrder> orderList = new ArrayList<VivoRetailOrder>();


        for (VivoRetailOrder order: retailorders) {
            order.setIsTaskInsert(true);
            order.setOrderCode(order.getOrderCode().substring(0, 14));
            order.setTableZone(area);
            if (StringUtils.isNotBlank(order.getPayType())) {
                order.setPayType("1");
            } else {
                order.setPayType("0");
            }
            orderList.add(order);

            if (orderList.size() >= 1000) {
                // 保存单据
                saveOrders(orderList, area);

                orderList.clear();
            }
        }

        if (orderList.size() > 0) {
            // 保存单据
            saveOrders(orderList, area);
        }
    }

    private void saveOrders(List<VivoRetailOrder> orderList, String area) {
        try {
            vivoRetailOrderService.insertByBatch(orderList, area);
        } catch (Exception ex) {
            ex.printStackTrace();
            // 存在重复项目
            for (VivoRetailOrder order: orderList) {
                try {
                    vivoRetailOrderService.save(order);
                } catch (Exception e) {
                    System.out.println("数据已经存在！ err:" + e.getMessage());
                }
            }
        }
    }
}
